﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace Mcmurphy.Common
{
    public static class PathHelper
    {
        /// <summary>
        /// 请求参数拆分成为文件列表
        /// </summary>
        /// <param name="queryContent"></param>
        /// <param name="fileExtension"></param>
        /// <returns></returns>
        public static string[] QueryToFileList(string queryContent, string fileExtension)
        {
            var originQuery = queryContent.Replace("\\", "/").Replace(" ", "");
            var pathSegments = new List<string>();
            var filePathBuilder = new StringBuilder(50);
            var isCurrentCharInPathGroup = false;
            foreach (char currentChar in originQuery)
            {
                if (currentChar == '[')
                {
                    isCurrentCharInPathGroup = true;
                    filePathBuilder.Append(currentChar);
                    continue;
                }
                if (currentChar == ']')
                {
                    isCurrentCharInPathGroup = false;
                    filePathBuilder.Append(currentChar);
                    pathSegments.Add(filePathBuilder.ToString());
                    filePathBuilder.Clear();
                    continue;
                }
                if (isCurrentCharInPathGroup)
                {
                    filePathBuilder.Append(currentChar);
                    continue;
                }
                if (currentChar == ',')
                {
                    if (filePathBuilder.Length > 0)
                    {
                        pathSegments.Add(filePathBuilder.ToString());
                        filePathBuilder.Clear();
                    }
                    continue;
                }
                filePathBuilder.Append(currentChar);
            }
            if (filePathBuilder.Length > 0)
            {
                pathSegments.Add(filePathBuilder.ToString());
                filePathBuilder.Clear();
            }

            var paths = new List<string>();
            //构造文件路径
            foreach (string pathSegment in pathSegments)
            {
                if (pathSegment.StartsWith("[") && pathSegment.EndsWith("]"))
                {
                    string clearedPathSegment = pathSegment.TrimStart('[').TrimEnd(']');
                    int splitterIndex = clearedPathSegment.LastIndexOf('/');
                    string pathPrefix = clearedPathSegment.Substring(0, splitterIndex + 1);
                    string[] pathContents = clearedPathSegment.Substring(splitterIndex + 1).Split(',');
                    paths.AddRange(pathContents.Select(pathContent => FormatPathString(pathPrefix + pathContent, fileExtension)));
                }
                else
                {
                    paths.Add(FormatPathString(pathSegment, fileExtension));
                }
            }

            return paths.Distinct().ToArray();
        }

        /// <summary>
        /// 格式化文件路径
        /// </summary>
        /// <param name="path"></param>
        /// <param name="fileExtension">.js .css</param>
        /// <returns></returns>
        public static string FormatPathString(string path, string fileExtension)
        {
            if (!path.StartsWith("/"))
            {
                path = "/" + path;
            }
            if (!path.StartsWith("~"))
            {
                path = "~" + path;
            }
            if (!path.EndsWith(fileExtension))
            {
                path += fileExtension;
            }
            return path.ToLower();
        }

        /// <summary>
        /// 将拼接中的字符串路径转换为本地路径
        /// </summary>
        /// <param name="filePaths"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string[] GetPhysicPaths(string[] filePaths, HttpRequestBase request)
        {
            for (var i = 0; i < filePaths.Length; i++)
            {
                var jsFile = filePaths[i];
                var fullPath = request.RequestContext.HttpContext.Server.MapPath(jsFile);
                if (File.Exists(fullPath))
                {
                    filePaths[i] = fullPath;
                }
            }
            return filePaths;
        }
    }
}
